/*
* @Author: suddev
* @Date:   2017-08-20 23:52:52
* @Last Modified by:   SUDDEV
* @Last Modified time: 2017-09-06 11:19:27
*/

'use strict';

// 表单里的错误提示
var formError = {
    show    : function(errorMsg){
        $('.error-item').show().find('.error-message').text(errorMsg);
    },
    hide    : function(){
        $('.error-item').hide().find('.error-message').text('');
    }
};

var iformError = {
    show    : function(errorMsg){
        $('.error-item-i').show().find('.error-message-i').text(errorMsg);
    },
    hide    : function(){
        $('.error-item-i').hide().find('.error-message-i').text('');
    }
};

//page逻辑部分
var page = {
    customerInfo : {

    },
    init : function () {
        this.bindEvent();
        var customerId = _tools.getUrlParam('customerId');
        if (_tools.validate(customerId,'require')) {
            $('#customerId').val(customerId);
            this.submit();
        }
    },
    bindEvent : function () {
        var _this = this;
        //登陆按钮的点击
        $('#submit').click(function(){
            _this.submit();
        });
        $('#i_submit').click(function(){
            _this.isubmit();
        });
    },
    // 提交表单
    submit : function () {
        var formData = {
        	customerId : $.trim($('#customerId').val())
        };
        //表单验证结果
        var validateResult = this.formValidate(formData);
        // 验证成功
        if (validateResult.status) {
        	formError.hide();
            //提交
            _info.input_search(formData,function(res){
            	// alert(JSON.stringify(res[0]));
            	var formData = res[0];
            	$('#customerId').val(formData.customerId);
            	$('#i_customerId').val(formData.customerId);
            	$('#name').val(formData.realname);
                formData.sex==='男'?formData.sex='m':formData.sex='f';
            	$('#sex[value='+formData.sex+']').prop("checked", "checked");
            	$('#birthday').val(formData.birthday);
            	$('#phone').val(formData.phone);
            	$('#cellphone').val(formData.cellphone);
            	$('#address').val(formData.address);
            	$('#startDate').val(formData.startDate);
            	$('#memo').val(formData.memo);
                $('#income-form').show();
            },function(errorMsg){
                $('#income-form').hide();
                $('#info-input-form')[0].reset();
                formError.show(errorMsg);
            });
        }
        //验证失败
        else{
            // 错误提示
            formError.show(validateResult.msg);
            $('#income-form').hide();
        }
    },
    // 提交表单
    isubmit : function () {
        var formData = {
        	customerId : $.trim($('#i_customerId').val()),
        	callTime : $.trim($('#i_callTime').val()),
        	callMemo : $.trim($('#i_callMemo').val())
        };
        //表单验证结果
        var validateResult = this.iformValidate(formData);
        // 验证成功
        if (validateResult.status) {
        	iformError.hide();
            //提交
            _income.income_input(formData,function(res){
            	// alert(JSON.stringify(res));
            	$.gritter.add({
            	// (string | mandatory) the heading of the notification
            	title: '信息',
            	// (string | mandatory) the text inside the notification
           	 	text: '客户来电记录成功',
            	// (string | optional) the image to display on the left
            	image: 'assets/img/ui-sam.jpg',
            	// (bool | optional) if you want it to fade out on its own or just sit there
            	sticky: false,
            	// (int | optional) the time you want it to be alive for before fading out
            	time: '',
            	// (string | optional) the class name you want to apply to that specific message
            	class_name: 'my-sticky-class'
        	});
            },function(errorMsg){
                iformError.show(errorMsg);
            });
        }
        //验证失败
        else{
            // 错误提示
            iformError.show(validateResult.msg);
        }
    },
    // 表单验证
    formValidate : function(formData){
        var result = {
            status  : false,
            msg     : ''
        };
        if (!_tools.validate(formData.customerId,'require')) {
            result.msg = '请输入客户ID';
            return result;
        }
        //通过验证，返回正确提示
        result.status = true;
        result.msg    = '验证通过';
        return result;
    },
    // 来电记录表单验证
    iformValidate : function(formData){
        var result = {
            status  : false,
            msg     : ''
        };
        if (!_tools.validate(formData.callTime,'require')) {
            result.msg = '请输入来电时间';
            return result;
        } 
        var reg = /^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[13579][26])00))-02-29)$/;
		var regExp = new RegExp(reg);
		if(!regExp.test(formData.callTime)){
　　         result.msg = '客户来电时间格式不正确，例:2012-01-01';	
　　			return result;	
		}
        if (!_tools.validate(formData.customerId,'require')) {
            result.msg = '来电者ID为空';
            return result;
        }  
        //通过验证，返回正确提示
        result.status = true;
        result.msg    = '验证通过';
        return result;
    }
}

$(function () {
   page.init(); 
});